﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using 立库托盘入库.bean;
using 立库托盘入库.Contacts;
using 立库托盘入库.MyInterface;

namespace 立库托盘入库.HttpUntil
{
    class MyHttpUtil
    {

        //登录网络访问
        public async static void HttpDenglu(LoginInterface Logininterface, string content)
        {
            try
            {
                string url = Contact.HTTP + Contact.LOGIN;
                Boolean b = true;
                Console.WriteLine(url);
                string result = "";
                HttpWebResponse resp = null;
                await Task.Run(() =>
                {
                    HttpWebRequest req = postLoginHttp(url, content);

                    try
                    {
                        resp = (HttpWebResponse)req.GetResponse();
                        b = true;
                    }
                    catch (WebException ex)
                    {
                        resp = (HttpWebResponse)ex.Response;
                        b = false;
                    }
                    Stream stream = resp.GetResponseStream();
                    //获取响应内容
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                    stream.Close();
                });
                Debug.WriteLine(result);
                if (b)
                {
                    Logininterface.HttpLoginOk(result, resp);
                }
                else
                {
                    Logininterface.HttpLoginNo(result, b);

                }
            }
            catch (Exception e)
            {
                Logininterface.HttpLoginNo(e.ToString(), false);
            }
            
        }

        //获取缓冲托盘列表网络访问
        public async static void getHttpPanData(MainInterface interfacePackage)
        {
            try
            {
                Boolean b = true;
                string url = Contact.HTTP + Contact.PAN_DATA;

                string result = "";
                await Task.Run(() =>
                {
                    

                    HttpWebResponse resp = null;

                    HttpWebRequest req = postHttp(url, "");
                    req.ServicePoint.Expect100Continue = false;
                    try
                    {
                        resp = (HttpWebResponse)req.GetResponse();
                        b = true;
                    }
                    catch (WebException ex)
                    {
                        resp = (HttpWebResponse)ex.Response;
                        b = false;
                    }
                    Stream stream = resp.GetResponseStream();
                    //获取响应内容
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                    stream.Close();
                    Debug.WriteLine(result);
                });
                if (b)
                {

                    interfacePackage.HttpGetPanDataOk(result);
                }
                else
                {
                    interfacePackage.HttpGetPanDataNo(result);

                }
            }
            catch (Exception e)
            {
                interfacePackage.HttpGetPanDataNo(e.ToString());
            }
        }

        //上传叉车入库员工数据
        public async static void postHttpForkliftRecord(MainInterface interfacePackage,String pallet_num, String uid)
        {
            try
            {
                Boolean b = true;
                string url = Contact.HTTP + Contact.FORKLIFT_RECORD;
                forkliftRecordBean bean = new forkliftRecordBean();
                bean.uid = uid;
                bean.pallet_num = pallet_num;
                string json = JsonConvert.SerializeObject(bean);
                string result = "";
                Debug.WriteLine(json);
                await Task.Run(() =>
                {
                    HttpWebResponse resp = null;
                    HttpWebRequest req = postHttp(url, json);
                    req.ServicePoint.Expect100Continue = false;
                    try
                    {
                        resp = (HttpWebResponse)req.GetResponse();
                        b = true;
                    }
                    catch (WebException ex)
                    {
                        resp = (HttpWebResponse)ex.Response;
                        b = false;
                    }
                    Stream stream = resp.GetResponseStream();
                    //获取响应内容
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                    stream.Close();
                    Debug.WriteLine(result);
                });
                
                if (b)
                {

                    interfacePackage.HttpPostForkliftRecordOk(result);
                }
                else
                {
                    interfacePackage.HttpPostForkliftRecordNo(result);

                }
            }
            catch (Exception e)
            {
                interfacePackage.HttpPostForkliftRecordNo(e.ToString());
            }

        }

        //取消二楼入库指令
        public async static void postHttpPallet_Finish_Cancel(MainInterface interfacePackage, String pallet_num)
        {
            try
            {
                Boolean b = true;
                string url = Contact.HTTP + Contact.FINISH_CANCEL;
                forkliftRecordBean bean = new forkliftRecordBean();
                bean.pallet_num = pallet_num;
                string json = JsonConvert.SerializeObject(bean);
                string result = "";
                await Task.Run(() =>
                {
                    HttpWebResponse resp = null;

                    HttpWebRequest req = postHttp(url, json);
                    req.ServicePoint.Expect100Continue = false;
                    try
                    {
                        resp = (HttpWebResponse)req.GetResponse();
                        b = true;
                    }
                    catch (WebException ex)
                    {
                        resp = (HttpWebResponse)ex.Response;
                        b = false;
                    }
                    Stream stream = resp.GetResponseStream();
                    //获取响应内容
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                    stream.Close();
                    Debug.WriteLine("ssssssssssssss" + result);
                });
                if (b)
                {

                    interfacePackage.HttpPostFinishCancelOk(result);
                }
                else
                {
                    interfacePackage.HttpPostFinishCancelNo(result);

                }
            }
            catch (Exception e)
            {
                interfacePackage.HttpPostFinishCancelNo(e.ToString());
            }
        }

        //取消二楼入库指令
        public async static void postHttpPallet_Shoudong_Finish_Cancel(MainInterface interfacePackage, String pallet_num)
        {
            try
            {
                Boolean b = true;
                string url = Contact.HTTP + Contact.FINISH_SHOUDONG;
                forkliftRecordBean bean = new forkliftRecordBean();
                bean.pallet_num = pallet_num;
                string json = JsonConvert.SerializeObject(bean);
                string result = "";
                await Task.Run(() =>
                {
                    HttpWebResponse resp = null;

                    HttpWebRequest req = postHttp(url, json);
                    req.ServicePoint.Expect100Continue = false;
                    try
                    {
                        resp = (HttpWebResponse)req.GetResponse();
                        b = true;
                    }
                    catch (WebException ex)
                    {
                        resp = (HttpWebResponse)ex.Response;
                        b = false;
                    }
                    Stream stream = resp.GetResponseStream();
                    //获取响应内容
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                    stream.Close();
                    Debug.WriteLine("ssssssssssssss" + result);
                });
                if (b)
                {

                    interfacePackage.HttpPostShoudongFinishCancelOk(result);
                }
                else
                {
                    interfacePackage.HttpPostShoudongFinishCancelNo(result);

                }
            }
            catch (Exception e)
            {
                interfacePackage.HttpPostFinishCancelNo(e.ToString());
            }
        }
        //获取Pda组员工列表
        public async static void getHttpPda_Username(MainInterface interfacePackage, String User_Name)
        {
            try
            {
                Boolean b = true;
                string url = Contact.HTTP + Contact.PDA_USERNAME;
                UserBean bean = new UserBean();
                bean.name = User_Name;
                string json = JsonConvert.SerializeObject(bean);
                string result = "";
                Debug.WriteLine("ssssssssssssss" + json);
                await Task.Run(() =>
                {
                    HttpWebResponse resp = null;

                    HttpWebRequest req = postHttp(url, json);
                    req.ServicePoint.Expect100Continue = false;
                    try
                    {
                        resp = (HttpWebResponse)req.GetResponse();
                        b = true;
                    }
                    catch (WebException ex)
                    {
                        resp = (HttpWebResponse)ex.Response;
                        b = false;
                    }
                    Stream stream = resp.GetResponseStream();
                    //获取响应内容
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                    stream.Close();

                });
                Debug.WriteLine("ssssssssssssss" + result);
                if (b)
                {

                    interfacePackage.HttpGetPdaUsernameOk(result);
                }
                else
                {
                    interfacePackage.HttpGetPdaUsernameNo(result);

                }
            }
            catch (Exception e)
            {
                interfacePackage.HttpGetPdaUsernameNo(e.ToString());
            }
        }

        //登录网络访问
        public static HttpWebRequest postLoginHttp(String url, string content)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Accept = "application/json";
            req.Headers.Add("X-CSRF-TOKEN", Login.token);
            req.CookieContainer = Login.cookiecon;
            #region 添加Post 参数
            byte[] data = Encoding.UTF8.GetBytes(content);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            return req;
        }

        public static HttpWebRequest postHttp(String url, string content)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Accept = "application/json";
            req.Headers.Add("X-CSRF-TOKEN", Login.token);
            req.CookieContainer = Login.cookiecon;

            req.Headers.Add("COOKIE", Login.COOKIE);
            Console.WriteLine(Login.token);
            Console.WriteLine(Login.COOKIE);
            #region 添加Post 参数
            byte[] data = Encoding.UTF8.GetBytes(content);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            return req;
        }

    }
}
